<?php
class StudentProfilesController extends AppController {
    var $name =  'StudentProfiles';
    var $uses = array('StudentProfile', 'Grade');
    
    function edit($id = null) {
        $this->pageTitle = "修改资料";
        $this->StudentProfile->id = $id;
        $grades = $this->Grade->find('all');
        $arr_for_options = array();
        foreach ($grades as $grade):
            $arr_for_options[$grade['Grade']['id']] = $grade['Grade']['enroll_year']."届".$grade['Grade']['class_number']."班";
        endforeach;
        $this->set('ops', $arr_for_options);
        if (($this->Auth->user('id') == $this->StudentProfile->field('user_id')) || ($this->Auth->user('role') == 'admin')) {
            if (!$this->data) {
                $this->data = $this->StudentProfile->read();
            }
            else {
                $this->StudentProfile->save($this->data);
                $this->Session->setFlash("资料修改成功");
                if ($this->Auth->user('role') == 'admin') {
                    $this->redirect(array('controller' => 'users', 'action' => 'add'));
                }
                else {
                    $this->redirect(array('controller' => 'student_profiles', 'action' => 'index'));
                }
            }
        }
        else {
            $this->Session->setFlash("您没有相应的权限");
        }
    }
    
    function view_as_teacher($id = null) {
        if ($this->Auth->user('role') == 'teacher') {
            $this->StudentProfile->id = $id;
            $this->set('the_student', $this->StudentProfile->read());
        }
        else {
            $this->Session->setFlash("您没有相应的权限");
            $this->redirect(array('controller' => 'users', 'action' => 'login'));
        }
    }
    
    function list_for_performance_add($grade_id = null) {
        $this->pageTitle = "学生列表";
        if ($this->Auth->user('role') == 'teacher') {
            $this->set('allstudents', $this->StudentProfile->findAllByGradeId($grade_id));
        }
        else {
            $this->Session->setFlash("您没有相应的权限");
            $this->redirect(array('controller' => 'users', 'action' => 'login'));
        }
    }
    
    function index() {
        $this->pageTitle = "学生管理面板";
        if ($this->Auth->user('role') == 'student') {
            $this->set('my_info', $this->StudentProfile->findByUserId($this->Auth->user('id')));
        }
        else {
            $this->Session->setFlash("您没有相应的权限");
            $this->redirect(array('controller' => 'users', 'action' => 'login'));
        }
    }
}
?>